<template>
  <component
    is="a-modal"
    :width="tool.getDevice() === 'mobile' ? '100%' : '600px'"
    v-model:visible="visible"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="直播间ID" field="room_id">
        <a-input v-model="formData.room_id" placeholder="请输入直播间ID" />
      </a-form-item>
      <a-form-item label="直播间名称" field="name">
        <a-input v-model="formData.name" placeholder="请输入直播间名称" />
      </a-form-item>
      <a-form-item label="1 并发 2 流量" field="pay_type">
        <a-input v-model="formData.pay_type" placeholder="请输入1 并发 2 流量" />
      </a-form-item>
      <a-form-item label="账号类型（1：主账号 2：子账号 ~~~~）" field="type">
        <a-input v-model="formData.type" placeholder="请输入账号类型（1：主账号 2：子账号 ~~~~）" />
      </a-form-item>
      <a-form-item label="消耗量" field="webinar_flow">
        <a-input v-model="formData.webinar_flow" placeholder="请输入消耗量" />
      </a-form-item>
      <a-form-item label="直播间人数" field="people">
        <a-input v-model="formData.people" placeholder="请输入直播间人数" />
      </a-form-item>
      <a-form-item label="总时长" field="total_time">
        <a-input v-model="formData.total_time" placeholder="请输入总时长" />
      </a-form-item>
      <a-form-item label="开播时间" field="start_time">
        <a-input v-model="formData.start_time" placeholder="请输入开播时间" />
      </a-form-item>
      <a-form-item label="直播结束时间" field="end_look_time">
        <a-input v-model="formData.end_look_time" placeholder="请输入直播结束时间" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import tool from '@/utils/tool'
import { Message, Modal } from '@arco-design/web-vue'
import api from '@/api/volume'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')

let title = computed(() => {
  return '微吼直播间流量消耗' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单初始值
const initialFormData = {
  id: null,
  room_id: null,
  name: '',
  pay_type: null,
  type: null,
  webinar_flow: '',
  people: null,
  total_time: null,
  start_time: null,
  end_look_time: null,
}

// 表单信息
const formData = reactive({ ...initialFormData })

// 验证规则
const rules = {
  name: [{ required: true, message: '直播间名称必需填写' }],
  pay_type: [{ required: true, message: '1 并发 2 流量必需填写' }],
  type: [{ required: true, message: '账号类型（1：主账号 2：子账号 ~~~~）必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  // 重置表单数据
  Object.assign(formData, initialFormData)
  formRef.value.clearValidate()
  visible.value = true
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
